﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using Data;

public class ActionUIRootPage : MonoBehaviour
{
    public Text groupSelect;
    
    public Action_BaseDataPanel action_basePanel;
    public ActionSubDataPanel actionSubPanel;
    public AnimListPanel animListPanel;
    public CameraModelCtr cameraModelCtr;
    public SelectGroupPanel selectGroupPanel;

    private Data.ActionGroup groupData;
    #region menue button
    /// <summary>
    /// 打开指定配置文件
    /// </summary>
    public void OpenConfig()
    {
        var configPath = FileActionIO.OpenFile();
        if (File.Exists(configPath))
        {
            if (EActionDataCtr.Instance.OpenTargetData(configPath))
            {
                groupData = EActionDataCtr.Instance.CurGroupData();
                //TODO :Init UI
                action_basePanel.OpenPanel(GroupSubData.GroupBaseData, groupData);
            }
        }
        else
        {
            FucTempLog.LogError(configPath);
        }
    }

    private void OnEnable()
    {
        EventManager.AddEventListener<Data.ActionGroup>(CommonEvent.SelectActionGroup, OnSelectActionGroup);
        EventManager.AddEventListener<Data.ActionGroup>(CommonEvent.RefreshCurActionGroup, RefreshCurActionGroup);
    }
    private void OnDisable()
    {
        EventManager.RemoveEventListener<Data.ActionGroup>(CommonEvent.RefreshCurActionGroup, RefreshCurActionGroup);
        EventManager.RemoveEventListener<Data.ActionGroup>(CommonEvent.RefreshCurActionGroup, RefreshCurActionGroup);
    }
    /// <summary>
    /// 刷新group基础信息
    /// </summary>
    /// <param name="newGroup"></param>
    private void RefreshCurActionGroup(Data.ActionGroup newGroup)
    {
        SetCurGroupSelectText();
    }
    /// <summary>
    /// 更新当前Group信息
    /// </summary>
    /// <param name="newGroup"></param>
    private void OnSelectActionGroup(Data.ActionGroup newGroup)
    {
        groupData = newGroup;
        SetCurGroupSelectText();
        action_basePanel.OpenPanel(GroupSubData.GroupBaseData, newGroup);
    }
    /// <summary>
    /// 打开动画文件列表
    /// </summary>
    public void OpenAnimationList()
    {
        var folderPath = FileActionIO.OpenFolderByUnity("选择动画列表", @"C:\Users\bambom\Desktop\DEV\OP\work\Tsesy\Tsesy_Stand_Empire\Assets\OutputBundle\Models");
        if (Directory.Exists(folderPath))
        {
            DirectoryInfo folderInfo = new DirectoryInfo(folderPath);
            //所有动画
            var anims = folderInfo.GetFiles("*.anim", SearchOption.TopDirectoryOnly);
            var modelPrefab = folderInfo.GetFiles("*.prefab", SearchOption.TopDirectoryOnly);
            var controller = folderInfo.GetFiles("*.controller", SearchOption.TopDirectoryOnly);

            //设置动画列表面板
            animListPanel.OpenAnimList(ParseFileName(anims));
            cameraModelCtr.SetModel(modelPrefab[0].FullName, controller[0].FullName);
        }
    }
    
    private string[] ParseFileName(FileInfo[] anims)
    {
        string[] names = new string[anims.Length];
        for (int i = 0; i < anims.Length; i++)
        {
            names[i] = Path.GetFileNameWithoutExtension(anims[i].FullName);
        }
        return names;
    }

    /// <summary>
    /// 保存当前动画文件
    /// </summary>
    public void SaveCurUnitActionInfo()
    {
        EActionDataCtr.Instance.SaveGroupData();
    }

    /// <summary>
    /// 点击选择打开Group列表面板
    /// </summary>
    public void SelectActionGroup()
    {
        selectGroupPanel.OpenSelectGroupPanel();
        groupData = EActionDataCtr.Instance.CurGroupData();
        SetCurGroupSelectText();
    }

    private void SetCurGroupSelectText()
    {
        groupSelect.text = string.Format("{0}_{1}", groupData.GroupNum, groupData.Race);
    }
    #endregion
}
